package cc.mrbird.febs.customer.controller;


import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.customer.entity.Customer;
import cc.mrbird.febs.customer.entity.Income;
import cc.mrbird.febs.customer.service.ICustomerService;
import cc.mrbird.febs.customer.service.IIncomeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前台用户表 前端控制器
 * </p>
 *
 * @author Macw
 * @since 2020-11-04
 */
@RestController
@RequestMapping("/customer")
public class CustomerController extends BaseController {

    @Resource
    private ICustomerService customerService;


    @Resource
    private IIncomeService incomeService;

    @RequestMapping("getCustomerList")
    @ControllerEndpoint(operation = "查询用户", exceptionMessage = "查询用户列表")
    public FebsResponse getCustomerList(Customer customer, QueryRequest request){
        Page<Customer> page = new Page<>(request.getPageNum(), request.getPageSize());
        IPage<Customer> page1 = customerService.page(page, new QueryWrapper<Customer>().lambda()
                .like(StringUtils.isNotBlank(customer.getNickname()), Customer::getNickname, customer.getNickname())
                .like(StringUtils.isNotBlank(customer.getTrueName()), Customer::getTrueName, customer.getTrueName())
                .eq(customer.getSex()!=null,Customer::getSex,customer.getSex())
                .orderByDesc(Customer::getCreateTime));
        return new FebsResponse().success().data(getDataTable(page1));
    }

    /**
     * 删除和批量删除
     * @param customerIds
     * @return
     */
    @GetMapping("delete/{customerIds}")
    @RequiresPermissions("customer:delete")
    @ControllerEndpoint(operation = "删除客户", exceptionMessage = "删除客户失败")
    public FebsResponse deleteActivity(@NotBlank(message = "{required}") @PathVariable String customerIds) {
        String[] ids = customerIds.split(StringPool.COMMA);
        //获取所有活动id列表
        List<String> list = Arrays.asList(ids);
        //删除活动
        customerService.removeByIds(list);
        return new FebsResponse().success();
    }

    @PostMapping("withDraw")
    @RequiresPermissions("income:update")
    @ControllerEndpoint(operation = "提现钱包", exceptionMessage = "提现钱包失败")
    public FebsResponse withDraw(Customer customer) {

        //本次提现金额
        double withdrawPrice = customer.getWithdrawPrice().doubleValue();
        //钱包余额
        double wallet = customer.getWallet().doubleValue();
        if (withdrawPrice > wallet || wallet<=0.00){
            return new FebsResponse().fail().message("余额不足");
        }
        try {
            wallet -= withdrawPrice;
            customer.setWallet(BigDecimal.valueOf(wallet));
            Income income = new Income();
            income.setStatus(0);
            income.setIncomePrice(BigDecimal.valueOf(withdrawPrice));
            income.setCustomerId(customer.getCustomerId());
            income.setCreateTime(LocalDateTime.now());
            income.setPid(0);
            incomeService.save(income);
            customerService.updateById(customer);

            //插入收入记录数据


        } catch (Exception e) {
            e.printStackTrace();
            return new FebsResponse().fail().message("系统繁忙");
        }
        return new FebsResponse().success();
    }


}
